package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.OrderComment;
import com.briup.jz.bean.OrderCommentExample;
import com.briup.jz.bean.extend.OrderCommentExtend;
import com.briup.jz.dao.OrderCommentMapper;
import com.briup.jz.dao.extend.OrderCommentExtendMapper;
import com.briup.jz.service.IOrderCommentService;
import com.briup.jz.utils.CustomerException;

@Service
public class OrderCommentServiceImpl implements IOrderCommentService {
	@Autowired
	private OrderCommentMapper orderCommentMapper;

	@Autowired
	private OrderCommentExtendMapper orderCommentExtendMapper;

	@Override
	public List<OrderComment> query(Long id) {
		OrderCommentExample example = new OrderCommentExample();
		if (id != null) {
			example.createCriteria().andIdEqualTo(id);
		}
		List<OrderComment> list = orderCommentMapper.selectByExample(example);
		return list;
	}

	@Override
	public void saveOrUpdate(OrderComment orderComment) throws CustomerException {
		if (orderComment.getId() == null) {
			orderCommentMapper.insert(orderComment);

		} else {
			orderCommentMapper.updateByPrimaryKey(orderComment);
		}
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		OrderComment orderComment = orderCommentMapper.selectByPrimaryKey(id);
		if (orderComment == null) {
			throw new CustomerException("要删除的信息不存在");
		}
		orderCommentMapper.deleteByPrimaryKey(id);

	}

	@Override
	public List<OrderComment> check(String content, String status, Long orderId) {
		// 模板查询
		OrderCommentExample example = new OrderCommentExample();
		OrderCommentExample.Criteria criteria = example.createCriteria();

		if (content != null) {
			criteria.andContentLike("%" + content + "%");
		}
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		if (orderId != null) {
			criteria.andOrderIdEqualTo(orderId);
		}
		return orderCommentMapper.selectByExample(example);
	}

	/*
	 * @Override public OrderCommentExtend read(long id) { // 根据id查询
	 * 
	 * return orderCommentExtendMapper.selectById(); }
	 */
	@Override
	public List<OrderCommentExtend> queryCascade(String content, String status, Long orderId) {
		// TODO Auto-generated method stub
		return orderCommentExtendMapper.select(content, status, orderId);
	}

	/* 分页 */
	@Override
	public List<OrderCommentExtend> pageQuery(int page, int pageSize, String content, String status, Long orderId) {
		int pageIndex = (page - 1) * pageSize;
		List<OrderCommentExtend> orderCommentExtends = orderCommentExtendMapper.pageQuery(pageIndex, pageSize, content,
				status, orderId);
		return orderCommentExtends;
	}

}
